﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Net.Mail;

namespace FoneBuddy.LinqData
{
    public partial class DataRepository : IDataRepository
    {
        protected FoneBuddyDbContext _dbFoneBuddy;

        public DataRepository()
        {
            _dbFoneBuddy = FoneBuddyDatabaseFactory.CreateDbContext();
        }

        private void PopulateModel(object sourceObj, object targetObj)
        {
            var sourceProperties = TypeDescriptor.GetProperties(sourceObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);

            var targetProperties = TypeDescriptor.GetProperties(targetObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);

            for (int i = 0; i < sourceProperties.Count(); i++)
            {
                targetProperties.ElementAt(i).SetValue(targetObj,
                    sourceProperties.ElementAt(i).GetValue(sourceObj));
            }
        }

        #region Country
        public List<Country> GetAllCountry()
        {
            var result = (from c in _dbFoneBuddy.Countries where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void CreateCountry(Country country)
        {
            _dbFoneBuddy.Countries.InsertOnSubmit(country);
            _dbFoneBuddy.SubmitChanges();
        }
        public void DeleteCountry(Country country)
        {
            Country deleteObj = (from u in _dbFoneBuddy.Countries
                                 where u.Id.Equals(country.Id)
                                 select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.Countries.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetCountryById(Country country)
        {
            var result = (from b in _dbFoneBuddy.Countries
                          where b.Id.Equals(country.Id) && b.IsDeleted == false
                          select b).First();

            if (result != null)
                PopulateModel(result, country);
        }
        public void UpdateCountry(Country country)
        {
            var result = _dbFoneBuddy.Countries.Where(x => x.Id == country.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = country.Name;
            }
            _dbFoneBuddy.SubmitChanges();

        }
        public List<Country> GetCountryByName(string CountryName)
        {
            var result = (from b in _dbFoneBuddy.Countries
                          where b.Name.Equals(CountryName) && b.IsDeleted == false
                          select b).ToList();

            return result;
        }
        #endregion

        #region State
        public List<State> GetAllState()
        {
            var result = (from c in _dbFoneBuddy.States where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void CreateState(State state)
        {
            _dbFoneBuddy.States.InsertOnSubmit(state);
            _dbFoneBuddy.SubmitChanges();
        }
        public void DeleteState(State state)
        {
            State deleteObj = (from u in _dbFoneBuddy.States
                               where u.Id.Equals(state.Id)
                               select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.States.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetStateById(State state)
        {
            var result = (from b in _dbFoneBuddy.States
                          where b.Id.Equals(state.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, state);
        }
        public void UpdateState(State state)
        {
            var result = _dbFoneBuddy.States.Where(x => x.Id == state.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = state.Name;
            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetStateByName(State state)
        {
            var result = (from b in _dbFoneBuddy.States
                          where b.Name.Equals(state.Name) && b.IsDeleted == false && b.CountryId.Equals(state.CountryId)
                          select b).FirstOrDefault();

            if (result != null)
                PopulateModel(result, state);
        }
        public List<State> GetStateByCountryId(State state)
        {
            var result = (from b in _dbFoneBuddy.States

                          where b.CountryId.Equals(state.CountryId) && b.IsDeleted == false
                          select b).ToList();

            return result;
        }
        #endregion

        #region City
        public List<City> GetAllCity()
        {
            var result = (from c in _dbFoneBuddy.Cities where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void CreateCity(City city)
        {
            _dbFoneBuddy.Cities.InsertOnSubmit(city);
            _dbFoneBuddy.SubmitChanges();
        }
        public void DeleteCity(City city)
        {
            City deleteObj = (from u in _dbFoneBuddy.Cities
                              where u.Id.Equals(city.Id)
                              select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.Cities.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetCityById(City city)
        {
            var result = (from b in _dbFoneBuddy.Cities
                          where b.Id.Equals(city.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, city);
        }
        public void UpdateCity(City city)
        {
            var result = _dbFoneBuddy.Cities.Where(x => x.Id == city.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = city.Name;
            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetCityByName(City city)
        {
            var result = (from b in _dbFoneBuddy.Cities
                          where b.Name.Equals(city.Name) && b.IsDeleted == false && b.CountryId.Equals(city.CountryId) && b.StateId.Equals(city.StateId)
                          select b).FirstOrDefault();

            if (result != null)
                PopulateModel(result, city);
        }
        public List<City> GetCityByStateId(City city)
        {
            var result = (from b in _dbFoneBuddy.Cities
                          where b.IsDeleted == false && b.StateId.Equals(city.StateId)
                          select b).ToList();

            return result;
        }
        #endregion

        #region Subscription
        public void CreateSubscription(Subscription subscription)
        {
            _dbFoneBuddy.Subscriptions.InsertOnSubmit(subscription);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<Subscription> GetAllSubscription()
        {
            var result = (from c in _dbFoneBuddy.Subscriptions where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteSubscription(Subscription subscription)
        {
            Subscription deleteObj = (from u in _dbFoneBuddy.Subscriptions
                                      where u.Id.Equals(subscription.Id)
                                      select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.Subscriptions.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetSubscriptionById(Subscription subscription)
        {
            var result = (from b in _dbFoneBuddy.Subscriptions
                          where b.Id.Equals(subscription.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, subscription);
        }
        public void UpdateSubscription(Subscription subscription)
        {
            var result = _dbFoneBuddy.Subscriptions.Where(x => x.Id == subscription.Id).FirstOrDefault();
            if (result != null)
            {
                result.PlanName = subscription.PlanName;
                result.Duration = subscription.Duration;
                result.Amount = subscription.Amount;
                result.Description = subscription.Description;
                result.UpdatedDate = subscription.UpdatedDate;
            }
            _dbFoneBuddy.SubmitChanges();
        }
        public void GetSubscriptionByName(Subscription subscription)
        {
            var result = (from b in _dbFoneBuddy.Subscriptions
                          where b.PlanName.Equals(subscription.PlanName) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, subscription);
        }


        #endregion

        #region Brand
        public void CreateBrand(Brand brand)
        {
            _dbFoneBuddy.Brands.InsertOnSubmit(brand);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<Brand> GetAllBrand()
        {
            var result = (from c in _dbFoneBuddy.Brands where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteBrand(Brand brand)
        {
            Brand deleteObj = (from u in _dbFoneBuddy.Brands
                               where u.Id.Equals(brand.Id)
                               select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.Brands.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetBrandById(Brand brand)
        {
            var result = (from b in _dbFoneBuddy.Brands
                          where b.Id.Equals(brand.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, brand);
        }
        public void UpdateBrand(Brand brand)
        {
            var result = _dbFoneBuddy.Brands.Where(x => x.Id == brand.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = brand.Name;
                result.UpdatedDate = brand.UpdatedDate;

            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetBrandByName(Brand brand)
        {
            var result = (from b in _dbFoneBuddy.Brands
                          where b.Name.Equals(brand.Name) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, brand);

        }
        #endregion

        #region Category
        public void CreateCategory(Category category)
        {
            _dbFoneBuddy.Categories.InsertOnSubmit(category);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<Category> GetAllCategory()
        {
            var result = (from c in _dbFoneBuddy.Categories where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteCategory(Category category)
        {
            Category deleteObj = (from u in _dbFoneBuddy.Categories
                                  where u.Id.Equals(category.Id)
                                  select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.Categories.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetCategoryById(Category category)
        {
            var result = (from b in _dbFoneBuddy.Categories
                          where b.Id.Equals(category.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, category);
        }
        public void UpdateCategory(Category category)
        {
            var result = _dbFoneBuddy.Categories.Where(x => x.Id == category.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = category.Name;
                result.UpdatedDate = category.UpdatedDate;

            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetCategoryByName(Category category)
        {
            var result = (from b in _dbFoneBuddy.Categories
                          where b.Name.Equals(category.Name) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, category);

        }
        #endregion

        #region OS
        public void CreateOs(OperatingSystemInfo os)
        {
            _dbFoneBuddy.OperatingSystemInfos.InsertOnSubmit(os);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<OperatingSystemInfo> GetAllOs()
        {
            var result = (from c in _dbFoneBuddy.OperatingSystemInfos where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteOs(OperatingSystemInfo os)
        {
            OperatingSystemInfo deleteObj = (from u in _dbFoneBuddy.OperatingSystemInfos
                                             where u.Id.Equals(os.Id)
                                             select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.OperatingSystemInfos.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetOsById(OperatingSystemInfo os)
        {
            var result = (from b in _dbFoneBuddy.OperatingSystemInfos
                          where b.Id.Equals(os.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, os);
        }
        public void UpdateOs(OperatingSystemInfo os)
        {
            var result = _dbFoneBuddy.OperatingSystemInfos.Where(x => x.Id == os.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = os.Name;
                result.UpdatedDate = os.UpdatedDate;

            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetOsByName(OperatingSystemInfo os)
        {
            var result = (from b in _dbFoneBuddy.OperatingSystemInfos
                          where b.Name.Equals(os.Name) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, os);

        }
        #endregion

        #region Color
        public void CreateColor(Color color)
        {
            _dbFoneBuddy.Colors.InsertOnSubmit(color);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<Color> GetAllColor()
        {
            var result = (from c in _dbFoneBuddy.Colors where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteColor(Color color)
        {
            Color deleteObj = (from u in _dbFoneBuddy.Colors
                               where u.Id.Equals(color.Id)
                               select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.Colors.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetColorById(Color color)
        {
            var result = (from b in _dbFoneBuddy.Colors
                          where b.Id.Equals(color.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, color);
        }
        public void UpdateColor(Color color)
        {
            var result = _dbFoneBuddy.Colors.Where(x => x.Id == color.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = color.Name;
                result.UpdatedDate = color.UpdatedDate;

            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetColorByName(Color color)
        {
            var result = (from b in _dbFoneBuddy.Colors
                          where b.Name.Equals(color.Name) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, color);

        }
        #endregion

        #region Camera
        public void CreateCamera(Camera camera)
        {
            _dbFoneBuddy.Cameras.InsertOnSubmit(camera);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<Camera> GetAllCamera()
        {
            var result = (from c in _dbFoneBuddy.Cameras where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteCamera(Camera camera)
        {
            Camera deleteObj = (from u in _dbFoneBuddy.Cameras
                                where u.Id.Equals(camera.Id)
                                select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.Cameras.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetCameraById(Camera camera)
        {
            var result = (from b in _dbFoneBuddy.Cameras
                          where b.Id.Equals(camera.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, camera);
        }
        public void UpdateCamera(Camera camera)
        {
            var result = _dbFoneBuddy.Cameras.Where(x => x.Id == camera.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = camera.Name;
                result.UpdatedDate = camera.UpdatedDate;

            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetCameraByName(Camera camera)
        {
            FilterDetail f = new FilterDetail();

            var result = (from b in _dbFoneBuddy.Cameras
                          where b.Name.Equals(camera.Name) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, camera);

        }
        #endregion

        #region Filter
        public void CreateFilter(FilterMaster filter)
        {
            _dbFoneBuddy.FilterMasters.InsertOnSubmit(filter);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<LinqModel.FilterMaster> GetAllFilter()
        {
            var result = (from c in _dbFoneBuddy.FilterMasters where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteFilter(FilterMaster filter)
        {
            FilterMaster deleteObj = (from u in _dbFoneBuddy.FilterMasters
                                      where u.Id.Equals(filter.Id)
                                      select u).SingleOrDefault();
            if (deleteObj != null)
            {
                _dbFoneBuddy.FilterMasters.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetFilterById(FilterMaster filter)
        {
            var result = (from b in _dbFoneBuddy.FilterMasters
                          where b.Id.Equals(filter.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, filter);
        }
        public void UpdateFilter(FilterMaster filter)
        {
            var result = _dbFoneBuddy.FilterMasters.Where(x => x.Id == filter.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = filter.Name;
                result.UpdatedDate = filter.UpdatedDate;

            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetFilterByName(FilterMaster filter)
        {
            var result = (from b in _dbFoneBuddy.FilterMasters
                          where b.Name.Equals(filter.Name) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, filter);

        }
        #endregion

        #region FilterDetail
        public void CreateFilterDetail(FilterDetail filterDetail)
        {
            _dbFoneBuddy.FilterDetails.InsertOnSubmit(filterDetail);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<LinqModel.FilterDetail> GetAllFilterDetail()
        {
            var result = (from c in _dbFoneBuddy.FilterDetails where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeleteFilterDetail(FilterDetail filterDetail)
        {
            FilterDetail deleteObj = (from u in _dbFoneBuddy.FilterDetails
                                      where u.Id.Equals(filterDetail.Id)
                                      select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.FilterDetails.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void GetFilterDetailById(FilterDetail filterDetail)
        {
            var result = (from b in _dbFoneBuddy.FilterDetails
                          where b.Id.Equals(filterDetail.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, filterDetail);
        }
        public void UpdateFilterDetail(FilterDetail filterDetail)
        {
            var result = _dbFoneBuddy.FilterDetails.Where(x => x.Id == filterDetail.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = filterDetail.Name;
                result.FilterId = filterDetail.FilterId;
                result.UpdatedDate = filterDetail.UpdatedDate;

            }
            _dbFoneBuddy.SubmitChanges();

        }
        public void GetFilterDetailByName(FilterDetail filterDetail)
        {
            var result = (from b in _dbFoneBuddy.FilterDetails
                          where b.Name.Equals(filterDetail.Name) && b.IsDeleted == false && b.FilterId.Equals(filterDetail.FilterId)
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, filterDetail);

        }
        public List<LinqModel.FilterDetail> GetAllFilterDetailByFilterId(FilterDetail filterDetail)
        {
            var result = (from c in _dbFoneBuddy.FilterDetails where c.IsDeleted == false && c.FilterId.Equals(filterDetail.FilterId) select c).ToList();
            return result;
        }
        #endregion

        #region Product

        public void CreateProduct(Product product)
        {
            _dbFoneBuddy.Products.InsertOnSubmit(product);
            _dbFoneBuddy.SubmitChanges();
        }

        public String GenerateProductCode()
        {
            var result = (from c in _dbFoneBuddy.Products where c.IsDeleted == false select c).OrderByDescending(w => w.Id).ToList();


            if (result.Count() <= 0)
            {

                return "PRD-00001";
            }
            else
            {
                Int32 ID = 0;
                ID = Convert.ToInt32(result.ElementAt(0).Id);
                return "PRD" + (ID+1).ToString("00000") + "";

            }
        }

        public List<LinqModel.Product> GetAllProduct()
        {
            var result = (from c in _dbFoneBuddy.Products where c.IsDeleted == false select c).ToList();
            return result;
        }

        public void GetProductByName(Product product)
        {
            var result = (from b in _dbFoneBuddy.Products
                          where b.Name.Equals(product.Name) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, product);
        }

        public void GetProductById(Product product)
        {
            var result = (from b in _dbFoneBuddy.Products
                          where b.Id.Equals(product.Id) && b.IsDeleted == false
                          select b).First();

            if (result != null)
                PopulateModel(result, product);
        }

        public void UpdateProduct(Product product)
        {
            var result = _dbFoneBuddy.Products.Where(x => x.Id == product.Id).FirstOrDefault();
            if (result != null)
            {
                #region GeneralFeature
                result.BrandId = product.BrandId;
                result.CategoryId = product.CategoryId;
                result.Name = product.Name;
                result.ColorId = product.ColorId;
                result.NetworkId = product.NetworkId;
                result.ReleaseDate = product.ReleaseDate;
                #endregion

                #region Platform

                result.OSId = product.OSId;
                result.Processor = product.Processor;
                result.GPU = product.GPU;
                result.OperatingFrequency = product.OperatingFrequency;
                result.Java = product.Java;
                result.Browser = product.Browser;

                #endregion

                #region Camera
                result.RearCamera = product.RearCamera;
                result.FrontCamera = product.FrontCamera;
                result.CameraFeature = product.CameraFeature;
                result.Flash = product.Flash;
                result.Video = product.Video;
                #endregion

                #region Data & Internet
                result.Speed_3g_4g = product.Speed_3g_4g;
                result.Edge = product.Edge;
                result.Gprs = product.Gprs;
                result.Wi_Fi = product.Wi_Fi;
                result.Bluetooth = product.Bluetooth;
                #endregion

                #region Memory
                result.InternalMemoryId = product.InternalMemoryId;
                result.ExpandableMemory = product.ExpandableMemory;
                result.Ram = product.Ram;
                result.Rom = product.Rom;
                #endregion

                #region Bettery
                result.BetteryType = product.BetteryType;
                result.MaxStandBy = product.MaxStandBy;
                result.Capacity = product.Capacity;
                result.MusicPlayTime = product.MusicPlayTime;
                result.TalkTime = product.TalkTime;

                #endregion

                #region Display
                result.DisplayColor = product.DisplayColor;
                result.DisplaySize = product.DisplaySize;
                result.DisplayType = product.DisplayType;
                result.DisplayFeature = product.DisplayFeature;
                result.Resolution = product.Resolution;

                #endregion

                #region Sound
                result.AlertType = product.AlertType;
                result.MMJack = product.MMJack;
                result.LoudSpeaker = product.LoudSpeaker;

                #endregion

                #region Multimedia
                result.MusicPlayer = product.MusicPlayer;
                result.VideoPlayer = product.VideoPlayer;
                result.Radio = product.Radio;
                #endregion

                #region Other Feature

                result.SimId = product.SimId;
                result.SimSize = product.SimSize;
                result.Sensors = product.Sensors;
                result.SARValue = product.SARValue;
                result.ImportantApps = product.ImportantApps;
                result.DocumentViewer = product.DocumentViewer;
                #endregion

                #region Body
                result.FormFactorId = product.FormFactorId;
                result.Dimention = product.Dimention;
                result.InputMethodId = product.InputMethodId;
                result.ProductWeight = product.ProductWeight;
                #endregion

                #region Connectivity
                result.Gps = product.Gps;
                result.Usb = product.Usb;
                result.Messaging = product.Messaging;
                #endregion
                result.UpdatedDate = product.UpdatedDate;
                result.IpUpdated = product.IpUpdated;
                _dbFoneBuddy.SubmitChanges();
            }


        }

        public void DeleteProduct(Product product)
        {
            var result = _dbFoneBuddy.Products.Where(x => x.Id == product.Id).FirstOrDefault();
            if (result != null)
            {
                result.IsDeleted = true;
                _dbFoneBuddy.SubmitChanges();
            }
        }
        #endregion

        #region ProductImage

        public void CreateAllProductImage(List<ProductImageInfo> productImage)
        {
            _dbFoneBuddy.ProductImageInfos.InsertAllOnSubmit(productImage);
            _dbFoneBuddy.SubmitChanges();
        }

        public void CreateProductImage(ProductImageInfo productImage)
        {
            _dbFoneBuddy.ProductImageInfos.InsertOnSubmit(productImage);
            _dbFoneBuddy.SubmitChanges();
        }

        public List<LinqModel.ProductImageInfo> GetProductImagesById(ProductImageInfo productImage)
        {
            var result = (from c in _dbFoneBuddy.ProductImageInfos where c.IsDeleted == false && c.ProductId.Equals(productImage.ProductId) select c).ToList();
            return result;
        }

        public void DeleteProductImage(ProductImageInfo productImage)
        {
            ProductImageInfo deleteObj = (from u in _dbFoneBuddy.ProductImageInfos
                                where u.Id.Equals(productImage.Id)
                                select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.ProductImageInfos.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }

        public List<LinqModel.ProductImageInfo> GetAllProductImage()
        {
            var result = (from c in _dbFoneBuddy.ProductImageInfos where c.IsDeleted == false select c).ToList();
            return result;
        }
        #endregion

        #region Merchant
        public void CreateMerchant(Merchant merchant)
        {
            merchant.Password=EncryptionDecryption.Encrypt(merchant.Password);
            _dbFoneBuddy.Merchants.InsertOnSubmit(merchant);
            _dbFoneBuddy.SubmitChanges();
        }
        public void GetMerchantByEmailId(Merchant merchant)
        {
            var result = (from b in _dbFoneBuddy.Merchants
                          where b.Email.Equals(merchant.Email) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, merchant);

        }
        public List<Merchant> GetAllMerchant()
        {
            var result = (from c in _dbFoneBuddy.Merchants 
                          join ms in _dbFoneBuddy.SubscriptionHistories on c.Id equals ms.MerchantId
                          where c.IsDeleted == false 
                          select c).ToList();
            return result;
        }
        public void UpdateLoginInfo(Merchant merchant)
        {
            var result = _dbFoneBuddy.Merchants.Where(x => x.Id == merchant.Id).FirstOrDefault();
            if (result != null)
            {
                result.LastLoginDate = DateTime.Now;
            }
            _dbFoneBuddy.SubmitChanges();
        }
        public void UpdateContectName(Merchant merchant)
        {
            var result = _dbFoneBuddy.Merchants.Where(x => x.Id == merchant.Id).FirstOrDefault();
            if (result != null)
            {
                result.Name = merchant.Name;
            }
            _dbFoneBuddy.SubmitChanges();
        }
        public void GetMerchantById(Merchant merchant)
        {
            var result = (from b in _dbFoneBuddy.Merchants
                          where b.Id.Equals(merchant.Id) && b.IsDeleted == false
                          select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, merchant);

        }
        public List<Merchant> GetAllActiveMerchant()
        {
            var result = (from c in _dbFoneBuddy.Merchants
                          join ms in _dbFoneBuddy.SubscriptionHistories on c.Id equals ms.MerchantId
                          where c.IsDeleted == false && ms.ExpiryDate>=DateTime.Today && ms.IsSuccess==true
                          select c).ToList();
            return result;
        }

        public string[] IsValidLogin(Merchant merchant)
        {
            var result = (from b in _dbFoneBuddy.Merchants
                          where b.Email.Equals(merchant.Email) && b.IsDeleted == false && b.IsAdmin==merchant.IsAdmin
                          select b).FirstOrDefault();

            string[] loginDetail = new string[2];
            //loginDetail[0] means it returns user is expired or not if it is expire then it returns true else it returns false 
            //liginDetails[1] returns Merchant id if it is exits in syatem..

            if (result != null)
            {
                SubscriptionHistory subHistory = new SubscriptionHistory();
                List<SubscriptionHistory> subHistories = new List<SubscriptionHistory>();
                subHistory.MerchantId = result.Id;
                subHistories = GetSubscriptionHistoryByMerchantId(subHistory);
                if ((EncryptionDecryption.Decrypt(result.Password)).Equals(merchant.Password) && subHistories.ElementAt(0).IsSuccess == true && subHistories.ElementAt(0).IsActive == true)
                {
                    if (subHistories.ElementAt(0).ExpiryDate < DateTime.Today)
                    {
                        loginDetail[0] = "true";

                    }
                    else
                    {
                        loginDetail[0] = "false";
                    }
                    loginDetail[1] = result.Id.ToString();
                }
                else
                {
                    loginDetail[0] = "false";
                    loginDetail[1] = "0";
                }
            }
            else
            {
                loginDetail[0] = "false";
                  loginDetail[1] = "0";
            }
            return loginDetail;
        }

        public List<Merchant> GetAllInActiveMerchant()
        {
            var result = (from c in _dbFoneBuddy.Merchants
                          join ms in _dbFoneBuddy.SubscriptionHistories on c.Id equals ms.MerchantId
                          where c.IsDeleted == false && ms.ExpiryDate < DateTime.Today && ms.IsSuccess == true
                          select c).ToList();
            return result;
        }
        #endregion

        #region Merchant Subscription
        public void CreateMerchantSubscription(SubscriptionHistory merchant)
        {
            _dbFoneBuddy.SubscriptionHistories.InsertOnSubmit(merchant);
            _dbFoneBuddy.SubmitChanges();
        }
        public void UpdatePaymentStatus(SubscriptionHistory subHistory)
        {
            var result = _dbFoneBuddy.SubscriptionHistories.Where(x => x.Id == subHistory.Id).FirstOrDefault();
            if (result != null)
            {
                result.TransactionId = subHistory.TransactionId;
                result.IsSuccess = subHistory.IsSuccess;
            }
            _dbFoneBuddy.SubmitChanges();
        }
        public void GetSubscriptionHistoryById(SubscriptionHistory  subHistory)
        {
            var result = (from b in _dbFoneBuddy.SubscriptionHistories
                          where b.Id.Equals(subHistory.Id) && b.IsDeleted == false
                          select b).First();
            if (result != null)
                PopulateModel(result, subHistory);
        }

        public List<SubscriptionHistory> GetSubscriptionHistoryByMerchantId(SubscriptionHistory subHistory)
        {
            var result = (from b in _dbFoneBuddy.SubscriptionHistories
                          where b.MerchantId.Equals(subHistory.MerchantId) && b.IsDeleted == false
                          select b).OrderByDescending(w => w.Id).ToList();
            return result;
        }
        #endregion

        #region Address
        public void CreateMerchantAddress(Address address)
        {
            _dbFoneBuddy.Addresses.InsertOnSubmit(address);
            _dbFoneBuddy.SubmitChanges();
        }
        public List<Address> GetAddressByMerchantId(Address address)
        {
            var result = (from b in _dbFoneBuddy.Addresses
                          where b.MerchantId.Equals(address.MerchantId) && b.IsDeleted == false
                          select b).OrderByDescending(w => w.Id).ToList();
            return result;
        }
        public void UpdateAddress(Address address)
        {
            var result = _dbFoneBuddy.Addresses.Where(x=>x.Id==address.Id ).FirstOrDefault();
            if (result != null)
            {
                result.Address1 = address.Address1;
                result.Address2 = address.Address2;
                result.Pincode = address.Pincode;
                result.StateId =address.StateId;
                result.CityId=address.CityId;

            }
            _dbFoneBuddy.SubmitChanges();
        }
        #endregion

        #region Promotion Code
        public void CreatePromotionCode(PromotionCode pCode)
        {
            _dbFoneBuddy.PromotionCodes.InsertOnSubmit(pCode);
            _dbFoneBuddy.SubmitChanges();
        }

        public string GeneratePromotionCode()
        {

            var result = (from c in _dbFoneBuddy.PromotionCodes select c).OrderByDescending(w => w.Id).OrderByDescending(w=>w.Id).ToList();


            if (result.Count() <= 0)
            {

                return "PC-" + System.Guid.NewGuid().ToString("N").Substring(0, 2) + result.ElementAt(0).Id.ToString("000");
            }
            else
            {
                //Int32 ID = 0;
                //ID = Convert.ToInt32(result.ElementAt(0).Id);
                //string[] s = result.ElementAt(0).Code.Split('-');

                //return "PRC-" + (Convert.ToInt32(s[1]) + 1).ToString("00000") + "";
                return "PC-"+System.Guid.NewGuid().ToString("N").Substring(0, 2)+ result.ElementAt(0).Id.ToString("000");

            }
             
        }

        ////Note: sp_GetPromotionalCode.

        //public List<sp_GetPromotionalCodeResult> get()
        //{
        //    var result = (from c in _dbFoneBuddy.GellAllPromotionCode
        //                  select c).tolist();

        //    return result;
        //}
        public List<SP_GetAllPromotionCodeResult> GetAllPromotionCodeResult()
        {
            var result=(from c in _dbFoneBuddy.SP_GetAllPromotionCode() select c).ToList();
            return result;
        }

        public List<PromotionCode> GetAllPromotionCode()
        {
            var result = (from c in _dbFoneBuddy.PromotionCodes where c.IsDeleted == false select c).ToList();
            return result;
        }
        public void DeletePromotionCode(PromotionCode pCode)
        {

            PromotionCode deleteObj = (from u in _dbFoneBuddy.PromotionCodes
                                             where u.Id.Equals(pCode.Id)
                                             select u).SingleOrDefault();
            if (deleteObj != null)
            {

                _dbFoneBuddy.PromotionCodes.DeleteOnSubmit(deleteObj);
                _dbFoneBuddy.SubmitChanges();
            }
        }
        public void  GetPromotionCodeByCode(PromotionCode pCode)
        {

            var result = (from b in _dbFoneBuddy.PromotionCodes
                          where b.Code.Equals(pCode.Code) && b.IsDeleted == false  && b.SubscriptionId.Equals(pCode.SubscriptionId)
                          select b).FirstOrDefault();
            

            if (result != null)
                PopulateModel(result, pCode);
        }

        public void GetPromotionCodeById(PromotionCode pCode)
        {

            var result = (from b in _dbFoneBuddy.PromotionCodes
                          where b.Id.Equals(pCode.Id) && b.IsDeleted == false
                          select b).FirstOrDefault();


            if (result != null)
                PopulateModel(result, pCode);
        }

        public void UpdateUsedCount(PromotionCode pCode)
        {
            var result = _dbFoneBuddy.PromotionCodes.Where(x => x.Id == pCode.Id).FirstOrDefault();
            if (result != null)
            {
                result.UsedCount = result.UsedCount + 1;
            }
            _dbFoneBuddy.SubmitChanges();
        }
        #endregion


        //#region MailMessage
        //public void SendMessage(string ToID, string Sub, string strMsg)
        //{
        //    SmtpClient client = new SmtpClient();
        //    client.DeliveryMethod = SmtpDeliveryMethod.Network;
        //    // client.EnableSsl = true;
        //    client.Host = "mail.thewirelesstoys.in";
        //    client.Port = 25;
        //    // setup Smtp authentication

        //    System.Net.NetworkCredential credentials =
        //        new System.Net.NetworkCredential("support@thewirelesstoys.in", "dharmesh123#");
        //    client.UseDefaultCredentials = false;
        //    client.Credentials = credentials;

        //    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        //    msg.From = new MailAddress("support@thewirelesstoys.in");
        //    msg.To.Add(new MailAddress(ToID));

        //    msg.Subject = Sub;
        //    msg.IsBodyHtml = true;

        //    msg.Body = strMsg;
        //    try
        //    {
        //        client.Send(msg);
        //    }
        //    catch (Exception ex)
        //    {
        //       // return false;
        //    }
        //   // return true;
        //}
        //#endregion
    }
}
